package eu.briano.suirvellancecamera;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class Log {

	public static void v(String tag,String message ){
		v(tag,message,null);
	}
	
	
	public static void v(String tag,String message , Throwable t){
		android.util.Log.v(tag,message,t);
		writeMessage(android.util.Log.VERBOSE,tag,message,t);		
	}

	public static void d(String tag,String message ){
		d(tag,message,null);
	}	
	
	
	public static void d(String tag,String message , Throwable t){
		android.util.Log.d(tag,message,t);
		writeMessage(android.util.Log.DEBUG,tag,message,t);		
	}

	public static void i(String tag,String message ){
		i(tag,message,null);
	}	

	
	public static void i(String tag,String message , Throwable t){
		android.util.Log.i(tag,message,t);
		writeMessage(android.util.Log.INFO,tag,message,t);		
	}

	public static void w(String tag,String message ){
		w(tag,message,null);
	}	
	
	
	public static void w(String tag,String message , Throwable t){
		android.util.Log.w(tag,message,t);
		writeMessage(android.util.Log.WARN,tag,message,t);
	}

	public static void e(String tag,String message ){
		e(tag,message,null);
	}	
	
	
	public static void e(String tag,String message , Throwable t){
		android.util.Log.e(tag,message,t);
		writeMessage(android.util.Log.ERROR,tag,message,t);
	}	
	
	public static void writeMessage(int level,  String tag,String message , Throwable t){
		if (level < android.util.Log.INFO ||
			tag == null || message == null)
			return;
		
		String workDir = Utils.getWorkBasePath();
		if (Utils.createDirs(workDir)){
			String logFileName = workDir+"log.txt";
			File logFile = new File(logFileName);
			try {
				PrintWriter log = new PrintWriter(new FileOutputStream(
					   logFile, 
					    true));
				
				String date = new Date().toGMTString();
				String item = date + " - " +level + " - "+ tag+ " - " +message;
				if (t != null)
					t.printStackTrace(log);
				log.append(item+"\n");
				log.flush();
			} catch (IOException e) {
				android.util.Log.e(Utils.logName,"logging error ",e);
			}
			
		}
		
		
		
		
	}

}
